//
//  SyncTimer.m
//  YiJuZhangShou
//
//  Created by 冷悦 on 16/3/3.
//  Copyright © 2016年 Uranus. All rights reserved.
//

#import "SyncTimer.h"
#import "NetworkingBaseManager.h"
#import "RequestGenerator.h"
#import "NetworkingManagerFactory.h"

@interface SyncTimer () <NetworkingBaseManagerAPISuccessCallbackDelegate>
@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, strong) NetworkingBaseManager *manager;

@end

static SyncTimer *_syncTimer = nil;

@implementation SyncTimer

#pragma mark - Initialize
+ (SyncTimer *)sharedInstance{
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        _syncTimer = [[SyncTimer alloc]init];
        
        if (_syncTimer != nil) {
            [_syncTimer setupTimer];
        }
    });
    
    return _syncTimer;
}


#pragma mark - Private
- (void)setupTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                  target:self
                                                selector:@selector(updateTimeMillis:)
                                                userInfo:nil
                                                 repeats:YES];
    [self.timer fire];
}

- (void)setTimeMillis:(long long)systemTimeMillis {
    @synchronized(self) {
        _systemTimeMillis = systemTimeMillis;
    }
}

- (void)updateTimeMillis:(NSTimer *)timer{
    long long currentTimeMillis = self.systemTimeMillis + 1000;
    [self setTimeMillis:currentTimeMillis];
}

- (void)manager:(NetworkingBaseManager *)manager callAPIDidSuccessWithParams:(NSDictionary *)responseParams{
    NSDictionary *responseDict = [responseParams objectForKey:@"data"];
    NSString *timestampStr = [responseDict objectForKey:@"timestamp"];
    long long timestamp = [timestampStr longLongValue];
    [self setTimeMillis:timestamp];
    [[NSNotificationCenter defaultCenter] postNotificationName:SyncTimerNotification object:nil];
}

- (void)syncTime{
    NSString *apiName = [[RequestGenerator sharedInstance] generateCommonApiWithName:@"app/timeStamp"];
    self.manager = [NetworkingManagerFactory generateNormalManager];
    self.manager.requestType = HTTP_GET;
    self.manager.successDelegate = self;
    self.manager.apiName = apiName;
    [self.manager loadRequest];
}

/**
 *  @author X
 *
 *  yyyy-MM-dd HH:mm:ss形式的标准时间字符串转化为时间戳
 *
 *  @param timeStr yyyy-MM-dd HH:mm:ss形式的标准日期字符串
 *
 *  @return 时间戳
 */
+ (long long)standardTimeStringTotimeStamp:(NSString *)timeStr{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *beginDate = [formatter dateFromString:timeStr];
    long long timeStamp = [beginDate timeIntervalSince1970]*1000;
    return timeStamp;
}
/**
 *  @author X
 *
 *  时间戳转化为yyyy-MM-dd HH:mm:ss形式的标准时间字符串
 *
 *  @param timeStamp 时间戳
 *
 *  @return 标准时间字符串 yyyy-MM-dd HH:mm:ss形式
 */
+ (NSString *)timeStampToStandardTimeString:(long long)timeStamp{
    
    NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *timeStr = [formatter stringFromDate:timeDate];
    return timeStr;
}

/**
 *  @author X
 *
 *  时间字符串转化为时间戳
 *
 *  @param timeStr yyyy-MM-dd形式的日期字符串
 *
 *  @return 时间戳
 */
+ (long long)timeStringToTimeStamp:(NSString *)timeStr{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *beginDate = [formatter dateFromString:timeStr];
    long long timeStamp = [beginDate timeIntervalSince1970]*1000;
    return timeStamp;
}

/**
 *  @author X
 *
 *  时间戳转化为yyyy-MM-dd形式的时间字符串
 *
 *  @param timeStamp 时间戳
 *
 *  @return 时间字符串 yyyy-MM-dd形式
 */
+ (NSString *)timeStampToTimeString:(long long)timeStamp{
    
    NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *timeStr = [formatter stringFromDate:timeDate];
    return timeStr;
}

/**
 *  @author X
 *
 *  时间戳转换date
 *
 *  @param timeStamp 时间戳
 *
 *  @return date
 */
+ (NSDate *)timeStampToDate:(long long)timeStamp{
    NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];
    return timeDate;
}

/**
 *  @author X
 *
 *  获取当前时间字符串（格式为 yyyy-MM-dd）
 *
 *  @return 当前时间字符串 格式为 yyyy-MM-dd
 */
+ (NSString *)getTodayTimeString{
    NSDate *timeDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
     [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] ];
    NSString *timeStr = [formatter stringFromDate:timeDate];
    return timeStr;
}

@end
